// 导航菜单
$(".Rside>.icon").mouseover(() => {
    $(".mainnav>.boxmain").show();
    $(".mainnav>.boxmain").mouseover(() => {
        $(".mainnav>.boxmain").show()
    })
})
$(".Rside>.icon").mouseout(() => {
    $(".mainnav>.boxmain").hide();
})
$(".mainnav>.boxmain").mouseout(() => {
    $(".mainnav>.boxmain").hide()
})

// 搜索框
$inp_search = $("header>.search input");
$inp_search.focus(e => {
    e = window.event;
    target = e.srcElement;
    target.onclick = () => {
        target.value == "请选择游戏搜索道具" && (target.value = "");
    }
    target.onblur = () => {
        target.value == "" && (target.value = "请选择游戏搜索道具");
    }
})

// 轮播
var j = 0;
timer = setInterval(() => {
    j++;
    j == 5 && (j = 0);
    var mj = -770 * j;
    $(".carousel>.imgbox").css({
        "left": `${mj}px`,
    })
    $($(".carousel>ul li")[j]).addClass("active").siblings().removeClass();
}, 2000)
$(".carousel").mouseover(() => {
    clearInterval(timer)
})
$(".carousel").mouseout(() => {
    timer = setInterval(() => {
        j++;
        j == 5 && (j = 0);
        var mj = -770 * j;
        $(".carousel>.imgbox").css({
            "left": `${mj}px`,
        })
        $($(".carousel>ul li")[j]).addClass("active").siblings().removeClass();
    }, 2000)
})
$(".carousel>ul li").mouseover(e => {
    e = window.event;
    j = $(e.srcElement).index();
    var mj = -770 * j;
    $(".carousel>.imgbox").css({
        "left": `${mj}px`,
    })
    $($(".carousel>ul li")[j]).addClass("active").siblings().removeClass();
})


console.log(11);
// 热卖推荐渲染.
$.getJSON("http://172.168.20.183/daojucheng-juchengpin/project/php/dj_index.php?key=?", "hide=loldata", data => {
    console.log(111);
    console.log(data);
    // var ran = range(data, 6);
    // data.forEach((obj, i) => {
    //     if (ran.indexOf(i) != -1) {
    //         for (var i in obj) {
    //             var dl = $(`<dl></dl>`);
    //             var dt = $(`<dt></dt>`);
    //             var dd = $(`<dd></dd>`);
    //             dt.append(`<a href='#'><img src='images/${obj.pic}'></a>`);
    //             dd.append(`<p><b>${obj.classify}</b><a>${obj.name}</a></p><p>QB价：<b>${obj.Qprice}</b></p><p class='red'>热卖推荐</p><a class='button'>立即抢购</a>`)
    //             dl.append(dt);
    //             dl.append(dd);
    //         }
    //         $(".sec2>.left>.box").append(dl);
    //     }
    // })
},"JSON")


// 每日惊喜倒计时
timechange = setInterval(() => {
    getTime();
}, 1000)
getTime();


// 猜你喜欢渲染
$.get("php/dj_index.php", "hide=loldata", data => {
    function favorite(arr) {
        if ($(".sec3>.left>.box").children()) {
            $(".sec3>.left>.box").children().remove()
        }
        data.forEach((obj, i) => {
            if (arr.indexOf(i) != -1) {
                for (var j in obj) {
                    var dl = $(`<dl></dl>`);
                    var dt = $(`<dt></dt>`);
                    var dd = $(`<dd></dd>`);
                    dt.append(`<a href='#'><img src='images/${obj.pic}'></a>`);
                    dd.append(`<p><a><b>${obj.classify}</b>${obj.name}</a></p><p>商城价：<b>${obj.Qprice}</b></p>`)
                    dl.append(dt);
                    dl.append(dd);
                }
                $(".sec3>.left>.box").append(dl)
            }
        })
    }
    var ran = range(data, 5);
    favorite(ran);
    $(".sec3>.left>.title>a").click(() => {
        var ran = range(data, 5);
        favorite(ran);
    })
}, "JSON")


// 游戏专区：英雄联盟
$.get("php/dj_index.php", "hide=loldata", data => {
    game(data, "lol");
}, "JSON")

// 游戏专区：穿越火线
$.get("php/dj_index.php", "hide=cfdata", data => {
    game(data, "cf");
}, "JSON")

// 游戏专区：剑灵
$.get("php/dj_index.php", "hide=bnsdata", data => {
    game(data, "jl");
}, "JSON")

// 游戏专区：御龙在天
$.get("php/dj_index.php", "hide=ylztdata", data => {
    game(data, "ylzt");
}, "JSON")


//游戏盒子
$.get("php/dj_index.php", "hide=gamedata", data => {

    data.forEach((obj, i) => {
        if (obj.classify == "竞技游戏") {
            boxgame(obj,"gamejj")
            // searchbox(obj,"gamejj")
        }
        if (obj.classify == "休闲游戏") {
            boxgame(obj,"gamexy")
        }
        if (obj.classify == "角色扮演") {
            boxgame(obj,"gamecos")
        }
        if (obj.classify == "网页游戏") {
            boxgame(obj,"gamewy")
        }
    })
}, "JSON")

//头部盒子渲染
function boxgame(obj,path) {
    var div = $(`<div class='lst'><div>`)
    for (var j in obj) {
        var a = $(`<a href='#' class='lnk spr'>${obj.name}</a>`)
        if (obj.hot == 1) {
            a.append(`<span class='ico hot spr'>hot</span>`)
        }
    }
    div.append(a);
    $(`.boxmain .${path}`).append(div)
}

// 搜索盒子渲染
function searchbox(obj,path){
    var dd=$(`<dd class='clear_fix'></dd>`)
    for(var j in obj){
        var a= $(`<a href>${obj.name}</a>`)
        var i= $(`<i>|</i>`);
        dd.append(a).append(i);
    }
    
    $(`.search .${path}`).append(dd);
}

// 随机数
function range(arr, num) {
    var array = [];
    while (array.length < num) {
        var r = parseInt(Math.random() * (arr.length - 1));
        if (array.indexOf(r) == -1) {
            array.push(r);
        }
    }
    return array;
}

// 游戏专区数据渲染
function game(data, gamename) {
    var arr1 = range(data, 8);
    var sub1 = arr1.slice(0, 4);
    var sub2 = arr1.slice(4);
    var arr2 = range(data, 5);
    var num = 0;
    data.forEach((obj, i) => {
        if (sub1.indexOf(i) != -1) {
            var li = $(`<li></li>`);
            var a = $(`<a href='#'><span><strong>${obj.name}</strong></span><span>微信价：<b class='red'>${obj.Qprice}元</b></span><span>Q币价：${obj.Vprice}QB</span><img src='images/${obj.pic}'></a>`);
            $(`.${gamename} .box .listtop`).append(li.append(a));
        }
        if (sub2.indexOf(i) != -1) {
            var li = $(`<li></li>`);
            var a = $(`<a href='#'><img src='images/${obj.pic}'></a>`);
            var div = $(`<div class='text'><p><a href='#'>${obj.name}</a></p><p class='red'>微信价：<b>${obj.Vprice}元</b></p><p>Q币价：${obj.Qprice}QB</p></div>`)
            li.append(a).append(div);
            $(`.${gamename} .box .listbot`).append(li);
        }
        if (arr2.indexOf(i) != -1) {
            num++;
            if (num == 1) {
                var dd = $(`<dd class='top'><i class='icon'>排名0${num}</i></dd>`);
                var div = $(`<div class='text'><p><a href='#'>${obj.name}</a></p><p class='red'><b>${obj.Qprice} QB</b></p></div>`)
                var a = $(`<a href='#'><img src='images/${obj.pic}'></a>`)
                dd.append(div).append(a);
                $(`.${gamename} .box .rank>dl`).append(dd);
            } else {
                var dd = $(`<dd><i class='icon'>0${num}</i></dd>`);
                var div = $(`<div class='text'><p><a href='#'>${obj.name}</a></p><p class='red'><b>${obj.Qprice} QB</b></p></div>`)
                var a = $(`<a href='#'><img src='images/${obj.pic}'></a>`)
                dd.append(div).append(a);
                $(`.${gamename} .box .rank>dl`).append(dd);
            }
        }
    })


}

// 每日惊喜定时器
function getTime() {
    var now = new Date();
    var end = new Date("2022/1/18 10:00:00");
    var scalc = (end - now) / 1000;

    var d = parseInt(scalc / 3600 / 24);
    var h = parseInt(scalc / 60 / 60 % 24);
    (h > 0 && h < 10) && (h = `0${h}`);
    var m = parseInt(scalc % 3600 / 60);
    (m > 0 && m < 10) && (m = `0${m}`);
    var s = parseInt(scalc % 60);
    (s > 0 && s < 10) && (s = `0${s}`);


    if (d <= 0 && h <= 0 && m <= 0 && s <= 0) {
        $(".sec2 span.time").html("已结束");
        $(".sec2 span.time").addClass("red")
    } else {
        $(".sec2 span.time").html("");
        $(".sec2 span.time").append(`<em class='num'>${d}</em>天<em class='num'>${h}</em>时<em class='num'>${m}</em>分<em class='num'>${s}</em>秒`)
    }
}